package com.kady.Dao.impl;


import com.kady.utils.JdbcUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class BaseDao {
    QueryRunner queryRunner = new QueryRunner();

    public long update(String sql,Object...objs) throws SQLException {
        long res = queryRunner.update(JdbcUtil.getConnection(),sql,objs);
        return res;
    }

    public <T> T selectOne(Class<T> clazz,String sql,Object...objs) throws SQLException {
        T t = queryRunner.query(JdbcUtil.getConnection(),sql,new BeanHandler<>(clazz),objs);
        return t;
    }

    public Map selectMapOne(String sql,Object...objs) throws SQLException {
        Map res = queryRunner.query(JdbcUtil.getConnection(),sql,new MapHandler(),objs);
        return res;
    }

    public <T> List<T> selectList(Class<T> clazz, String sql,Object...objs) throws SQLException {
        List<T> list = queryRunner.query(JdbcUtil.getConnection(),sql,new BeanListHandler<>(clazz),objs);
        return list;
    }

    public List<Map<String,Object>> selectInnerList(String sql,Object...objs) throws SQLException {
        List<Map<String,Object>> list = queryRunner.query(JdbcUtil.getConnection(),sql,new MapListHandler(),objs);
        return list;
    }

    public <T> List<T> selectListByName(Class<T> clazz, String sql,Object...objs) throws SQLException {
        List<T> list = queryRunner.query(JdbcUtil.getConnection(),sql,new BeanListHandler<>(clazz),objs);
        return list;
    }
    public long selectCount(String sql,Object...objs) throws SQLException {
        return queryRunner.query(JdbcUtil.getConnection(),sql,new ScalarHandler<>(),objs);
    }
    
}
